home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1997 September / Macworld (1997-09).dmg / Serious Software / Cherwell Scientific Demos / pro Fit / pro Fit 5.0 demo (fpu).sea / pro Fit 5.0 demo (fpu) / External Modules / External modules sources / C / readSelection.c < prev    next >
Text File  |  1996-06-01  |  6KB  |  207 lines

  1. /***************************************************************************************/
  2. /* readSelection.c                                                                       */
  3. /*                                                                                     */
  4. /*                                                                                     */
  5. /* Version 1.6.1995                                                                     */
  6. /***************************************************************************************/
  7.  
  8.  
  9. #include "proFit_interface.h" 
  10. #ifndef _SPEECH_
  11.     #include <Speech.h>
  12. #endif
  13. #ifndef __GESTALTEQU__
  14.     #include <GestaltEqu.h>
  15. #endif
  16.  
  17. #include <string.h>
  18.  
  19.  
  20. static double dabs(double x) {return x<0 ? -x:x;}
  21.  
  22.  
  23. static Boolean SpeechAvailable (void) {
  24.     OSErr            err;
  25.     long            result;
  26.     err = Gestalt(gestaltSpeechAttr, &result);
  27.     if ((err != noErr) || !(result &  (1 << gestaltSpeechMgrPresent)))
  28.         return FALSE;
  29.     else
  30.         return TRUE;
  31. }
  32.  
  33. void SetUp (    short* const moduleKind,        /* set moduleKind to isFunction or isProgram */
  34.                 Str255 name,                    /* the name of the program or function (pascal string) */
  35.                 long* const requiredGlobals,    /* the number of bytes to be allocated in ExtModulesParamBlock.globals */
  36.                                                 /* set requiredGlobals to 0 if you don't use this feature */
  37.                 ExtModulesParamBlock* pb)        /* the complete parameter block passed by pro Fit to the */
  38.                                                 /* routines defined in this file. In most cases it can be ignored */
  39. /* SetUp is called once when the external module is linked to proFit */
  40. {
  41.     *moduleKind=isProgram;                        /* we define a program */
  42.     SetPascalStr(name,"\pRead Selection",255);    /* its name */
  43.     *requiredGlobals = 0;                        /* we don't need global data */
  44. }
  45.  
  46.  
  47. void InitializeProg (ExtModulesParamBlock* pb)
  48.     /* Can be left emtpy if not needed. */
  49.     /* called when the external module is linked to proFit after SetUp was called */
  50.     /* can be used to inititialize global variables, etc. */
  51. {
  52.         if (!SpeechAvailable())
  53.             {    SysBeep(0);
  54.                 Writeln("\p");
  55.                 Writeln("\pThis module requires the speech manager extension.");
  56.             }
  57.         else
  58.             SpeakString("\pHi, my job is reading numbers. Make a selection in the data window and choose my name from the menu misk.");
  59.  
  60. }
  61.  
  62. void Run(ExtModulesParamBlock* pb)
  63. /* pro Fit calls this function when the name of the program is chosen from the */
  64. /* Run Program submenu in the menu Calc */
  65. {    
  66.     if (!SpeechAvailable())
  67.             {    SysBeep(0);
  68.                 Writeln("\p");
  69.                 Writeln("\pThis module requires the speech manager extension.");
  70.             }
  71.     else
  72.     {    short    decs = 5;
  73.         Rect    sel = GetSelection();
  74.         OSErr    erry;
  75.         long    i, j;
  76.         Str255    stopmessage;
  77.         Str255    emptymessage;
  78.         Str255    minus;
  79.         Str255    s;
  80.         char    cs[256];
  81.         char*    pos1;
  82.         char*    pos2;
  83.         char*    k;
  84.         int        len;
  85.         
  86.         SetPascalStr(emptymessage,"\pEmpty.",255);
  87.         SetPascalStr(stopmessage,"\pOho! I was interrupted!",255);
  88.         SetPascalStr(minus,"\p minus ",255);
  89.         for (i = sel.top ; i<= sel.bottom;i++)
  90.             for (j = sel.left; j<= sel.right; j++)
  91.             {    GetCell(s,i,j);
  92.                 if (s[0]>0)
  93.                 {    if (TestData(i,j) && GetData(i, j)==0.0)
  94.                         SetPascalStr(s,"\pzero",20);
  95.                     else if (TestData(i,j))    /* the following is only needed if we read numbers */
  96.                     {
  97.                         pos1 = &cs[0];
  98.                         BlockMove(s+1,pos1,s[0]);
  99.                         cs[(s[0])+1] = 0;    /* cs is now a C string, and pos1 points into it. */
  100.                         len = s[0];
  101.                         
  102.                         s[0] = 0;            /* s will now become our destination string */
  103.                         
  104.                         pos2 = strchr(pos1, '+');        // take out a first '+'
  105.                         if ((long)pos2 > 0)
  106.                         {
  107.                             *pos2 = ' ';
  108.                             pos2 = strchr(pos1, '+');    // take out a second '+'
  109.                             if ((long)pos2 > 0)
  110.                                 *pos2 = ' ';
  111.                         }
  112.                         
  113.                         pos2 = strchr(pos1, '-');        // replace a first '-'
  114.                         if ((long)pos2 > 0)
  115.                         {
  116.                             BlockMove(pos1, &s[s[0] + 1], pos2 - pos1);
  117.                             s[0] += pos2 - pos1;
  118.                             BlockMove(&minus[1], &s[s[0] + 1], minus[0]);
  119.                             s[0] += minus[0];
  120.                             pos1 = pos2 + 1; 
  121.                             pos2 = strchr(pos1, '-');    // replace a second '-'
  122.                             if ((long)pos2 > 0)
  123.                             {
  124.                                 BlockMove(pos1, &s[s[0] + 1], pos2 - pos1);
  125.                                 s[0] += pos2 - pos1;
  126.                                 BlockMove(&minus[1], &s[s[0] + 1], minus[0]);
  127.                                 s[0] += minus[0];
  128.                                 pos1 = pos2 + 1; 
  129.                             }
  130.                         }
  131.                         BlockMove(pos1, &s[s[0] + 1], &cs[0] - pos1 + len);
  132.                         s[0] += &cs[0] - pos1 + len;
  133.                         
  134.                         pos1 = (char*)&s[1]; s[s[0] + 1] = 0;
  135.                         pos2 = strchr(pos1, 'e');        // break at 'e'
  136.                         if ((long)pos2 > 0)
  137.                         {
  138.                             for (k = (char*)&s[0] + s[0]; k > pos2; k--)
  139.                                 *(k+2) = *k;
  140.                             s[0] += 2;
  141.                             *pos2++ = ';'; *pos2++ = 'e'; *pos2++ = ';';
  142.                         }
  143.                         {    short n,m=1;    /* this block takes care of taking away unnecessary zeros */
  144.                             while (m<s[0] && s[m]!='.') m++;
  145.                             if (m<s[0])    /* if a decimal point was found */
  146.                             {    n=m;
  147.                                 m++;
  148.                                 while (m<s[0] && s[m]>='0' && s[m]<='9') m++; /* go to the end of the mantissa */
  149.                                 if (m<s[0]) m--;
  150.                                 while (m>n && s[m]=='0') s[m--]=' '; /* substitue unnecessary zeros with a space */
  151.                                 if (s[m]=='.') s[m]=' ';    /* kill the decimal point if it is the only thing left */
  152.                             }
  153.                         }
  154.                     }
  155.                     erry = SpeakString(s);
  156.                 }
  157.                 else
  158.                     erry = SpeakString(emptymessage);
  159.  
  160.                 if (erry) {SysBeep(0);return;}
  161.                 
  162.                 while    (SpeechBusy()) erry=TestStop();
  163.                 
  164.                 if (erry) 
  165.                 {    while    (SpeechBusy() && !Button()){};
  166.                     erry = SpeakString (stopmessage);
  167.                     return;
  168.                 }
  169.             }/* for i, for y */
  170.     } /*else*/
  171.  
  172. }
  173.  
  174. void CleanUp (ExtModulesParamBlock* pb)
  175.     /* called when the function or program is removed from pro Fit's menus */
  176.     /* in most cases, this function can be empty */
  177. {
  178.         if (SpeechAvailable())
  179.             SpeakString("\pGoodbye. It has been a pleasure to work for you.");
  180.  
  181. }
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191. /***************************************************************************************/
  192.                         /* for functions, not used here: */
  193. /***************************************************************************************/
  194.  
  195. void InitializeFunc (Boolean* const hasDerivatives, Str255 descr1stLine, Str255 descr2ndLine,        
  196.                     short* const numberOfParams, DefaultParamInfo* const a0, ExtModulesParamBlock* pb)
  197. {}
  198. void Func (    double x, ParamArray a,    double* const y, ExtModulesParamBlock* pb)        
  199. {}
  200. void Derivatives(double x, ParamArray a, ParamArray dyda, ExtModulesParamBlock* pb)
  201. {}
  202. short Check(short paramNo, DefaultParamInfo* const a0, ExtModulesParamBlock* pb)
  203. {return ok;}
  204. void First (ParamArray a, ExtModulesParamBlock* pb)
  205. {}
  206. void Last (ExtModulesParamBlock* pb)
  207. {}